<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/8/23 0023
 * Time: 10:27
 */namespace app\admin\model;
 use think\Db;

 class LeaveModel
 {
     /**获取一个请假模板
      * @return $this
      */
     static public function getLeave(){
         return Db::view('leave',true)
             ->view('user','nickname','leave.userid=user.id','left')
             ->view('company','company_name','leave.company_id=company.id','left')
             ->view('departmen_table','departmen_name,number','leave.departmen_id=departmen_table.id','left')
             ->json(['leave_picture']);
     }

     /**添加请假记录
      * @param $data
      * @return int|string
      */
     static public function addLeave($data){
         return Db::name('leave')->json(['leave_picture'])->insert($data);
     }

     /**添加请假记录并获取添加的id
      * @param $data
      * @return int|string
      */
     static public function addLeaveGetId($data){
         return Db::name('leave')->json(['leave_picture'])->insertGetId($data);
     }

     /**删除请假数据
      * @param $field
      * @param null $op
      * @param null $condition
      * @return int
      */
     static public function delLeave($field, $op = null, $condition = null){
         $sul = Db::connect();
         $datas = $sul->name('leave')->json(['leave_picture'])->where($field,$op,$condition)->select();
         foreach ($datas as $data){
             if(is_array($data['leave_picture'])){
                 foreach ($data['leave_picture'] as $value){
                     if(is_file(config('FileRoot').$value)){
                         unlink(config('FileRoot').$value);
                     }
                 }
             }
         }
         return $sul->name('leave')->where($field,$op,$condition)->delete();
     }

     /**更新数据
      * @param $data
      * @return int|string
      */
     static public function updateLeave($data){
         $sul = Db::connect();
         if(isset($data['leave_picture'])&&is_array($data['leave_picture'])){
             $datas = $sul->name('leave')->json(['leave_picture'])->where('id',$data['id'])->find();
             if(is_array($datas['leave_picture'])){
                 foreach ($datas['leave_picture'] as $value){
                     if(is_file(config('FileRoot').$value)){
                         unlink(config('FileRoot').$value);
                     }
                 }
             }
         }
         return $sul->name('leave')->json(['leave_picture'])->update($data);
     }
 }